/**
 * Copyright 2016-2018 mobaas.com
 */
package com.mobaas.devops.schedule.job;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.mobaas.devops.service.WebUrlService;

/**
 * 
 * @author billy zhang
 * 
 */
public class WebUrlCleanJob extends AbstractJob {

    private static final Logger logger = LoggerFactory.getLogger(WebUrlCleanJob.class);

    @Autowired
    private WebUrlService weburlService;

	@Override
	protected void doExecute(Map<String, Object> jobData) {
		
		int saveDays = (Integer)jobData.get("saveDays");
		
		logger.info("开始清除扫描URL日志...");
        
	    	Date beforeDate = getDate(saveDays);
	    	int n = weburlService.clearWebUrlLogs( beforeDate );
	
	    	logger.info("清除扫描URL记录" + n + "条，" + dtFmt.format(beforeDate) );
    	
	}
	
	 private static final SimpleDateFormat dtFmt = new SimpleDateFormat("yyyy-MM-dd");
	    
	 private Date getDate(int days) {
	   	Calendar cal = Calendar.getInstance();
	   	cal.set(Calendar.HOUR_OF_DAY, 0);
	   	cal.set(Calendar.MINUTE, 0);
	   	cal.set(Calendar.SECOND, 0);
	   	cal.set(Calendar.MILLISECOND, 0);
	   	cal.add(Calendar.DAY_OF_MONTH, 0 - days);
	   	
	   	return cal.getTime();
  }
}
